package com.taobao.aft.execute;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import com.taobao.aft.exception.AofTranslateException;
import com.taobao.aft.format.AofHSetCmdFormatBuilder;
import com.taobao.aft.format.AofLPushCmdFormatBuilder;
import com.taobao.aft.format.AofRPushCmdFormatBuilder;
import com.taobao.aft.format.AofSAddCmdFormatBuilder;
import com.taobao.aft.format.AofSelectCmdFormatBuilder;
import com.taobao.aft.format.AofSetCmdFormatBuilder;
import com.taobao.aft.format.AofZAddCmdFormatBuilder;
import com.taobao.inter.SplitInterface;
import com.taobao.inter.TranslateStrategy;

public class AofFormatTranslator {

	private static String inputfilename = null;
	private static String outputfilename = null;
	private static int index = 0;
	
	private static TranslateStrategy strategy = new TranslateStrategy() {
		@Override
		public void play(FileInputStream inputStream, FileOutputStream outputStream)
				throws IOException, AofTranslateException {
			AofSelectCmdFormatBuilder select = new AofSelectCmdFormatBuilder();
			AofSetCmdFormatBuilder set = new AofSetCmdFormatBuilder();
			
			BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
			outputStream.write(select.getAofFormat(String.valueOf(index)));
			
			String line = null;
			byte[] cmd = null;
			while((line = br.readLine()) != null) {
				if("".equals(line)) {
					continue;
				}
				cmd = set.getAofFormat(line);
				outputStream.write(cmd);
			}
		}
	};
	
	private static void init(String inputfilestr) {
		outputfilename = inputfilestr + ".out";
		AofSetCmdFormatBuilder.setSplitFunc(new SplitInterface() {
			
			@Override
			public String[] split(String data) {
				String[] array = data.split(",");
				if(array == null || array.length < 4) {
					return null;
				}
				String[] out = new String[2];
				out[0] = array[0].substring(1, array[0].length() - 1) +
						array[1].substring(1, array[1].length() - 1);
				out[1] = array[2] + "," + array[3];
				return out;
			}
		});
	}
	
	public static void setSelectSplit(SplitInterface split) {
		AofSelectCmdFormatBuilder.setSplitFunc(split);
	}
	public static void setSetSplit(SplitInterface split) {
		AofSetCmdFormatBuilder.setSplitFunc(split);
	}
	public static void setSaddSplit(SplitInterface split) {
		AofSAddCmdFormatBuilder.setSplitFunc(split);
	}
	public static void setHsetSplit(SplitInterface split) {
		AofHSetCmdFormatBuilder.setSplitFunc(split);
	}
	public static void setZSetSplit(SplitInterface split) {
		AofZAddCmdFormatBuilder.setSplitFunc(split);
	}
	public static void setLPushSplit(SplitInterface split) {
		AofLPushCmdFormatBuilder.setSplitFunc(split);
	}
	public static void setRPushSplit(SplitInterface split) {
		AofRPushCmdFormatBuilder.setSplitFunc(split);
	}

	public static void setStrategy(TranslateStrategy st) {
		strategy = st;
	}
	
	private static void doJob() {
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream(inputfilename);
			fos = new FileOutputStream(outputfilename);
			strategy.play(fis, fos);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException ee) {
			ee.printStackTrace();
		} catch (AofTranslateException eee) {
			eee.printStackTrace();
		} finally {
			if (fis != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	private static void end() {
		System.out.println("success ok");
	}
	
	/**
	 * @param args
	 */
	public static void play(String[] args) {
		if(args.length != 2) {
			System.out.println("args length != 3");
			return;
		}
		inputfilename = args[0];
		try {
			index = Integer.valueOf(args[1]);
		} catch (NumberFormatException e) {
			System.out.println("args error");
			return;
		}
		init(inputfilename);
		doJob();
		end();
	}
}
